<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\DB;

class BaseOrderService
{
    public function simpleConditions($query,$conditions){
        if (!empty($conditions['start_time'])) {
            $query->where('create_time', '>=', $conditions['start_time']);
        }

        if (!empty($conditions['end_time'])) {
            $query->where('create_time', '<=', $conditions['end_time']);
        }

        if (!empty($conditions['order_status'])) {
            if(strpos($conditions['order_status'],',') != false){
                $status_arr = explode(',',$conditions['order_status']);
                $query->whereIn('order_status', $status_arr);
            }else{
                $query->where('order_status', $conditions['order_status']);
            }
        }

        if (!empty($conditions['refund_reason'])) {
            $query->where('refund_reason','like','%'.$conditions['refund_reason'].'%');
        }

        if (!empty($conditions['order_id'])) {
            $query->where('order_id', $conditions['order_id']);
        }

        if (!empty($conditions['liver'])) {
            $query->where('liver', $conditions['liver']);
        }

        if (!empty($conditions['shop_id'])) {
            if(is_array($conditions['shop_id'])){
                $query->whereIn('shop_id', $conditions['shop_id']);
            }else{
                $query->where('shop_id', $conditions['shop_id']);
            }
        }

        if(!empty($conditions['back_status'])){
            //有回填数据的
            if($conditions['back_status'] == 1){
                $query->where('cost_price','!=',0);
            }
            //没有回填数据的
            if($conditions['back_status'] == 2){
                $query->where('cost_price','==',0);
            }
        }

        return $query->whereNotIn('order_status',[1,4]);
    }

    /*
     * 订单状态转为文字
     * */
    public function tranOrderStatusToText($order_status)
    {
        if (!empty($this->order_status_arr)) {
            foreach ($this->order_status_arr as $item) {
                if ($order_status == $item['order_status']) {
                    return $item['text'];
                }
            }
            return '未知';
        }
    }

    /*
     * 处理进度转为文字
     * */
    public function tranOrderProgressToText($progress)
    {
        if (!empty($progress)) {
            if($progress == 1){
                return '平台已退款';
            }
            if($progress == 2){
                return '未处理';
            }
            if($progress == 3){
                return '线下已退款';
            }
            return '未知';
        }
    }

    /*
     * 归并订单状态
     * */
    public function mergeOrderStatusToText($order_status)
    {
        if($order_status == 1){
            return '待确认';
        }
        if($order_status == 2){
            return '备货中';
        }
        if($order_status == 3){
            return '已发货';
        }
        if($order_status == 4){
            return '已取消';
        }
        if(in_array($order_status,[5,9,15,20,26,27,28,29,31,33,36,37,38])){
            return '已完成';
        }
        if(in_array($order_status,[12,14])){
            return '未发货退款';
        }
        if(in_array($order_status,[6,7,8,10,11,13,16,17,18,19,21,22,23,24,30,32,34,35,39])){
            return '已发货退款';
        }
        if(in_array($order_status,[21,22])){
            return '退款成功';
        }
    }


}
